import { VuexModule, Module, Mutation, Action, getModule } from "vuex-module-decorators";
import store from "@/store";
import defaultSettings from "@/settings";
import { Language, ContentTypes, ThemeSettings as ThemeSettingsCookie, FeaturesConfig } from '@/utils/auth';

export default class ThemeSettings {
  public bigScreen_title: string;
  public browser_title: string;
  public primary_color: string;

  constructor() {
    this.bigScreen_title = "移动云云桌面管理平台";
    this.browser_title = "移动云云桌面管理平台";
    this.primary_color = "#337DFF";
  }
}

export interface ISettingsState {
  language: string;
  themeSettings: ThemeSettings;
  fixedHeader: boolean;
  showSettings: boolean;
  showTagsView: boolean;
  showSidebarLogo: boolean;
  sidebarTextTheme: boolean;
  tunnySetting: any;
  publicSetting: any;
  ldapConfig: any;
  contentTypes: any;
  featuresConfig: any;
  refreshTokenTime: number;
}

@Module({ dynamic: true, store, name: "settings" })
class Settings extends VuexModule implements ISettingsState {
  public language = Language.get() || defaultSettings.language;
  public themeSettings = JSON.parse(ThemeSettingsCookie.get()) || defaultSettings.themeSettings;
  public fixedHeader = defaultSettings.fixedHeader;
  public showSettings = defaultSettings.showSettings;;
  public showTagsView = defaultSettings.showTagsView;
  public showSidebarLogo = defaultSettings.showSidebarLogo;
  public sidebarTextTheme = defaultSettings.sidebarTextTheme;
  public tunnySetting = defaultSettings.tunnySetting;
  public publicSetting = defaultSettings.publicSetting;
  public ldapConfig = defaultSettings.ldapConfig;
  public contentTypes = JSON.parse(ContentTypes.get()) || defaultSettings.contentTypes;
  public featuresConfig = JSON.parse(FeaturesConfig.get()) || defaultSettings.featuresConfig;
  public refreshTokenTime = defaultSettings.refreshTokenTime;

  @Mutation
  private CHANGE_SETTING(payload: { key: string; value: any }) {
    const { key, value } = payload;
    if (Object.prototype.hasOwnProperty.call(this, key)) {
      (this as any)[key] = value;
    }
  }

  @Mutation
  private SET_TUNNY_SETTING(tunnySetting: any) {
    if (!tunnySetting) {
      return false;
    }

    this.tunnySetting.auth_failed_max_times = tunnySetting.auth_failed_max_times;
    this.tunnySetting.auth_failed_try_later = tunnySetting.auth_failed_try_later;
    this.tunnySetting.enable_complex_password = tunnySetting.enable_complex_password;
  }

  @Mutation
  private SET_PUBLIC_SETTING(publicSetting: any) {
    if (!publicSetting) {
      return false;
    }

    this.publicSetting = publicSetting;
  }

  @Mutation
  private SET_LDAP_CONFIG(ldapConfig: any) {
    if (!ldapConfig) {
      return false;
    }

    this.ldapConfig = ldapConfig;
  }

  @Mutation
  private SET_CONTENT_TYPES(contentTypes: any) {
    if (!contentTypes) {
      return false;
    }

    this.contentTypes = contentTypes;
  }

  @Mutation
  private SET_THEME_SETTING(themeSetting: any) {
    if (!themeSetting) {
      return false;
    }
    if (!themeSetting.browser_title || themeSetting.browser_title === "undefined") {
      themeSetting.browser_title = defaultSettings.themeSettings.browser_title;
      // console.log(themeSetting.browser_title, defaultSettings.themeSettings.browser_title);
    }

    this.themeSettings = themeSetting;
  }

  @Mutation
  private SET_FEATURES_CONFIG(featuresConfig: any) {
    if (!featuresConfig) {
      return false;
    }

    this.featuresConfig = featuresConfig;
  }

  @Action
  public ChangeSetting(payload: { key: string; value: any}) {
    this.CHANGE_SETTING(payload);
  }

  @Action
  public setTunnySetting(options: any) {
    this.SET_TUNNY_SETTING(options);
  }

  @Action
  public setPublicSetting(options: any) {
    this.SET_PUBLIC_SETTING(options);
  }

  @Action
  public setLdapConfig(options: any) {
    this.SET_LDAP_CONFIG(options);
  }

  @Action
  public setContentTypes(contentTypes: any) {
    ContentTypes.set(JSON.stringify(contentTypes));
    this.SET_CONTENT_TYPES(contentTypes);
  }

  @Action
  public setThemeSettings(themeSettings: any) {
    ThemeSettingsCookie.set(JSON.stringify(themeSettings));
    document.body.style.setProperty('--color-primary', themeSettings.primary_color);
    this.SET_THEME_SETTING(themeSettings);
  }

  @Action
  public getContentTypeId(model: string) {
    const id = this.contentTypes.find((ct: any) => ct.model === model)?.id;
    return id;
  }

  @Action
  public setFeaturesConfig(featuresConfig: any) {
    FeaturesConfig.set(JSON.stringify(featuresConfig));
    this.SET_FEATURES_CONFIG(featuresConfig);
  }
}

export const SettingsModule = getModule(Settings);
